package com.durablemethods.futures.server;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;
import java.util.logging.Logger;

import com.durablemethods.futures.client.NHLTeam;
import com.durablemethods.futures.client.NHLTeamService;
import com.google.appengine.api.rdbms.AppEngineDriver;
import com.google.gwt.user.server.rpc.RemoteServiceServlet;

public class NHLTeamServiceImpl extends RemoteServiceServlet implements NHLTeamService {
	private static final long serialVersionUID = -4695852125091291402L;
	private static final String CONNECTION_STRING = "jdbc:google:rdbms://durablemethods.com:duravest:durable-futures-db";
	private static final Logger log = Logger.getLogger(NHLTeamServiceImpl.class.getName());
	@Override
	public List<NHLTeam> getAllTeams() {
		ArrayList<NHLTeam> returnList = new ArrayList<NHLTeam>();
		Connection c = null;
	      try {	    	 
			DriverManager.registerDriver(new AppEngineDriver());
			c = DriverManager.getConnection(CONNECTION_STRING,"root","");
			Statement s = c.createStatement();
			ResultSet rs = s.executeQuery("Select * from durable_futures.TEAMS where status<>'E'  order by standings_position");
			while (rs.next()){
				NHLTeam team = new NHLTeam();
				team.setId(rs.getString("id"));
				team.setCity(rs.getString("name"));
				team.setPosition(rs.getInt("standings_position"));
				team.setConference(rs.getString("conference"));
				String string = rs.getString("status");
				if (string.equalsIgnoreCase("E")){
					team.setEliminated(true);
				}
				returnList.add(team);
			}
			s.close();
			c.close();
		} catch (SQLException e) {
			log.info(e.getMessage());
			e.printStackTrace();
			
		}
		return returnList;
	}

}
